package com.wdd.atcrowdfunding.potal.controller;

import com.wdd.atcrowdfunding.bean.*;
import com.wdd.atcrowdfunding.manager.service.TagService;
import com.wdd.atcrowdfunding.manager.service.TypeService;
import com.wdd.atcrowdfunding.potal.service.*;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private MemberProjectFollowService memberProjectFollowService;
    @Autowired
    private ProjecttypeService projecttypeService;
    @Autowired
    private ProjectTagService projectTagService;
    @Autowired
    private ProjectMemberImgService projectMemberImgService;
    @Autowired
    private ReturnService returnService;



    /**
     * 跳转到我的众筹
     * @return
     */
    @RequestMapping("/minecrowdfunding")
    public String minecrowdfunding(){
        return "project/minecrowdfunding";
    }


    /**
     * 发起众筹 协议条款页面
     * @return
     */
    @RequestMapping("/start")
    public String start(){
        return "project/start";
    }


    /**
     * 发起众筹 第一步
     * @return
     */
    @RequestMapping("/start-step-1")
    public String index(Model model){
        //获取分类
        List<Type> typeList = typeService.selectAll();
        //获取标签
        List<Tag> tagList = tagService.selectAll();
        //将标签分为父子标签
        List<Tag> root = new ArrayList<Tag>();
        Map<Integer,Tag> map = new HashMap<Integer,Tag>();//100
        for (Tag innertag : tagList) {
            map.put(innertag.getId(), innertag);
        }
        for (Tag tag : tagList) { //100
            //通过子查找父
            //子菜单
            Tag child = tag ; //假设为子菜单
            if(child.getPid() == null ){
                root.add(tag);
            }else{
                //父节点
                Tag parent = map.get(child.getPid());
                parent.getChildren().add(child);
            }
        }


        model.addAttribute("typeList",typeList);
        model.addAttribute("tagRoot",root);
        return "project/start-step-1";
    }

    /**
     * 保存项目
     * @return
     */
    @RequestMapping("/inPro")
    @ResponseBody
    public AjaxResult inPro(@RequestParam("file") MultipartFile[] files, String type, String tag, Project project, MemberProjectFollow memberProjectFollow,
                        HttpSession session) throws IOException {

        AjaxResult ajaxResult = new AjaxResult();
        //获取memberid
        Member member = (Member) session.getAttribute(Const.LOGIN_MEMBER);
        //创建时间
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        project.setCreatedate(dateString);
        project.setMemberid(member.getId());
        //保存项目
        projectService.insert(project);
        Integer id = project.getId();//返回主键


        memberProjectFollow.setMemberid(member.getId());
        memberProjectFollow.setProjectid(id);
        //保存MemberProjectFollow
        memberProjectFollowService.insert(memberProjectFollow);

        //保存projectType
        ProjectType projectType = new ProjectType();
        projectType.setProjectid(id);
        projectType.setTypeid(Integer.parseInt(type));
        projecttypeService.insert(projectType);

        //保存projectTag
        ProjectTag projectTag = new ProjectTag();
        projectTag.setProjectid(id);
        projectTag.setTagid(Integer.parseInt(tag));
        projectTagService.insert(projectTag);

        //保存图片
        String realPath = session.getServletContext().getRealPath("/pics");
        for(MultipartFile fileImg : files){
            ProjectMemberImg projectMemberImg = new ProjectMemberImg();
            String extName = fileImg.getOriginalFilename().substring(fileImg.getOriginalFilename().lastIndexOf("."));
            String uuidName = UUID.randomUUID().toString();
            String filename = realPath + "/proj" + "/" + uuidName + extName;
            fileImg.transferTo(new File(filename));

            projectMemberImg.setIconpath(uuidName + extName);
            projectMemberImg.setMemberid(member.getId());
            projectMemberImg.setProjectid(id);

            projectMemberImgService.insert(projectMemberImg);
        }
        session.setAttribute("project",project);
        ajaxResult.setSuccess(true);

        return ajaxResult;
    }

    @RequestMapping("/start-step-2")
    public String start_step_2(){
        return "project/start-step-2";
    }


    /**
     * 添加回报
     * @param rt
     * @param session
     * @return
     */
    @RequestMapping("/addRt")
    @ResponseBody
    public AjaxResult addRt(Return rt,HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        Project project = (Project) session.getAttribute("project");
        rt.setProjectid(project.getId());
        returnService.insert(rt);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 加载projectid的回报
     * @param session
     * @return
     */
    @RequestMapping("/selectByPid")
    @ResponseBody
    public AjaxResult selectByPid(HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        Project project = (Project) session.getAttribute("project");
        List<Return> returnList = returnService.selectByPid(project.getId());
        ajaxResult.setSuccess(true);
        ajaxResult.setData(returnList);
        return ajaxResult;
    }


    /**
     * 删除回报
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String id){
        AjaxResult ajaxResult = new AjaxResult();
        returnService.deleteById(id);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }


    @RequestMapping("/start-step-3")
    public String start_step_3(){
        return "project/start-step-3";
    }

    @RequestMapping("/start-step-4")
    public String start_step_4(String alipay,HttpSession session){
        Project project = (Project) session.getAttribute("project");
        project.setAlipay(alipay);
        projectService.insertAlipay(project);
        return "project/start-step-4";
    }
}
